<?php
/**
 * 公共接口
 * User: cxm
 * Date: 2020/9/28
 * Time: 13:32
 */

namespace App\Http\Controllers\Common;
use App\Http\Controllers\Controller;
use App\Models\Adv;
use App\Models\File;
use App\Models\SystemConfig;
use Illuminate\Http\Request;

class CommonController extends Controller
{
    /**
     * 关于我们
     * URL POST:/api/common/aboutUs
     * @return mixed
     */
    public function aboutUs(){
        $result = SystemConfig::getSingleValue('about_us');
        return $this->success('查询成功',$result);
    }

    /**
     * 获取指定的配置信息
     * URL POST:/api/common/getConfig
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getConfig(Request $request){
        $params = $request->only(['code']);
        if (!array_key_exists('code',$params) || empty($params['code'])) {
            return $this->error('缺少标识码');
        }
        $value = SystemConfig::getSingleValue($params['code']);
        $type=  SystemConfig::getSingleType($params['code']);
        if($type == 'image' || $type == 'file'){
            $path  = File::where('id', $value)->value('path');
            return $this->success('查询成功',$path);
        }else{
            return $this->success('查询成功',$value);
        }
    }

    /**
     * 广告接口
     * URL POST:/api/common/getAdv
     * @param Request $request
     * @return mixed
     */
    public function getAdv(Request $request){
        $params = $request->only(['page','limit']);
        // 基础数据
        $data = [
            'index'                     => [], // 首页背景图
            'activity_detail'           => [], // 活动详情页
        ];
        // 广告列表
        $params['where'][] = ['status',1];
        // 图片
        $params['with'][] = 'file:id,path';
        $advData = Adv::getAll($params);
        foreach ($advData as $key=>$item) {
            // 查看是否设置了有效期
            if ( $item['start_time'] && $item['end_time'] ) {// 设置了有效期
                if (time() >= strtotime($item['start_time']) && time() <= strtotime($item['end_time'])) { // 在范围之类
                    if ($item['position_id'] == 1) { // 首页背景图
                        $data['index'][] = $item;
                    } else if ($item['position_id'] == 2) { // 活动详情页
                        $data['activity_detail'][] = $item;
                    }
                }
            } else {
                if ($item['position_id'] == 1) { // 首页轮播图 --用户端PC
                    $data['index'][] = $item;
                } else if ($item['position_id'] == 1) { // 活动详情页
                    $data['activity_detail'][] = $item;
                }
            }
        }

        return $this->success('查询成功',$data);
    }
}